//
//  DataTool.m
//  mengbutou
//
//  Created by Jiafei Wei on 16/7/6.
//  Copyright © 2016年 Jiafei Wei. All rights reserved.
//

#import "DataTool.h"

@implementation DataTool
+(NSString *)getStringDate:(NSDate *)date{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm";
    return [formatter stringFromDate:date];
}
+(NSDate *)dateFromString:(NSString *)dateString{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm"];
    NSDate *destDate= [dateFormatter dateFromString:dateString];
    return destDate;
    
}
+(long)getTimestamp:(NSDate *)date{
    return (long)[date timeIntervalSince1970];
}
+(NSString *)getStringTimestamp:(NSDate *)date{
    long  datelong = (long)[date timeIntervalSince1970];
    NSString *timeSp = [NSString stringWithFormat:@"%ld",datelong];
    return timeSp;
}
+(NSString *)timestampChaStringDate:(long)timeStamp{
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyy-MM-dd HH:mm";
        NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timeStamp];
        NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
        return confromTimespStr;
}
/**
 *
 */
+(BOOL)dateCompareToNowDate:(NSDate *)date{
    NSDate * now = [NSDate date];
    NSTimeInterval timeBetween = [now timeIntervalSinceDate:date];
    if (timeBetween>0){
        return NO;
    }else{
        return YES;
    }
}

/****
 ios比较日期大小默认会比较到秒
 ****/
+(int)compareOneDay:(NSDate *)oneDay withAnotherDay:(NSDate *)anotherDay
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd-MM-yyyy HH:mm"];
    NSString *oneDayStr = [dateFormatter stringFromDate:oneDay];
    NSString *anotherDayStr = [dateFormatter stringFromDate:anotherDay];
    NSDate *dateA = [dateFormatter dateFromString:oneDayStr];
    NSDate *dateB = [dateFormatter dateFromString:anotherDayStr];
    
    NSComparisonResult result = [dateA compare:dateB];
    NSLog(@"date1 : %@, date2 : %@", oneDay, anotherDay);
    if (result == NSOrderedDescending) {
        //NSLog(@"Date1  is in the future");
        return 1;
    }
    else if (result ==NSOrderedAscending){
        //NSLog(@"Date1 is in the past");
        return -1;
    }
    //NSLog(@"Both dates are the same");
    return 0;
}

//获取时间差
+(NSDateComponents *)getDateDifference:(NSDate *)date withAnotherDay:(NSDate*)anotherDay by:(int)type{
    NSCalendar *cal = [NSCalendar currentCalendar];
    unsigned int unitFlags =type;
    NSDateComponents *d = [cal components:unitFlags fromDate:anotherDay toDate:date options:0];
    return  d;
}

+(NSString *)timestampChaStringDate2:(long)timeStamp{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy/MM/dd";
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timeStamp];
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    return confromTimespStr;
}

+(NSDate *)timestampChangeToDate:(long)timeStamp{
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timeStamp];
    return confromTimesp;
}
+(NSString*)tampChangeToHour:(int)t{
        int h = t/3600;
        int ppp = t%3600;
        int m =ppp/60;
        int s =ppp%60;
    NSString *timeS = [NSString stringWithFormat:@"%@%@%@%@%@",[NSString stringWithFormat:@"%i",h],@":",[NSString stringWithFormat:@"%i",m],@":",[NSString stringWithFormat:@"%i",s]];
    return timeS;
}
+(NSDateComponents *)getNSDateComponents:(long)time{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSDateComponents *dateComponent = [calendar components:unitFlags fromDate: [self timestampChangeToDate:time]];
    return dateComponent;
}
@end
